package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Candidate;
import model.VoteDetails;
import encryption.Paillier;

public class VotePanel extends JPanel implements ActionListener {

	private ClientFrame frame;
	private JLabel voteLabel;
	private JButton submitButton;
	// private JRadioButton candidates;
	private JComboBox candidatesbox;
	private List<Candidate> candidatesList;
	private String[] candidatesName;

	@SuppressWarnings("unchecked")
	public VotePanel(ClientFrame f) {
		super(new BorderLayout());

		frame = f;
		this.setBounds(0, 0, frame.getWidth(), frame.getHeight());
		this.setBackground(Color.WHITE);
		this.setLayout(null);

		voteLabel = new JLabel("Choose your candidate:");
		voteLabel.setBounds(300, 150, 300, 20);
		this.add(voteLabel);

		try {
			Socket connectionSocket = new Socket("127.0.0.1", 4444);
			ObjectOutputStream objectOut = new ObjectOutputStream(
					connectionSocket.getOutputStream());
			ObjectInputStream objectIn = new ObjectInputStream(connectionSocket
					.getInputStream());

			objectOut.writeObject("get candidates list");
			objectOut.flush();
			candidatesList = (List<Candidate>) objectIn.readObject();

			candidatesName = new String[candidatesList.size()];

			for (int i = 0; i < candidatesName.length; i++) {
				candidatesName[i] = candidatesList.get(i).getName();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		candidatesbox = new JComboBox(candidatesName);
		candidatesbox.setBounds(300, 180, 150, 20);
		candidatesbox.setSelectedIndex(0);
		candidatesbox.addActionListener(this);
		this.add(candidatesbox);

		submitButton = new JButton("Submit");
		submitButton.setBounds(300, 220, 90, 20);
		submitButton.addActionListener(this);
		this.add(submitButton);
		this.repaint();

		frame.setContentPane(this);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == submitButton) {

			try {
				Socket connectionSocket = new Socket("127.0.0.1", 4444);
				ObjectOutputStream objectOut = new ObjectOutputStream(
						connectionSocket.getOutputStream());
				ObjectInputStream objectIn = new ObjectInputStream(
						connectionSocket.getInputStream());

				objectOut.writeObject("get public key");
				objectOut.flush();
				BigInteger[] publicKey = (BigInteger[]) objectIn.readObject();

				VoteDetails voteDetails = new VoteDetails();

				String encryptedVote = (Paillier.encrypt(candidatesList.get(
						candidatesbox.getSelectedIndex()).getBigIntCode(),
						publicKey[0], publicKey[1])).toString();

				voteDetails.setEncryptedVote(encryptedVote);
				voteDetails.setUserSession(this.frame.getUserSession());
				objectOut.writeObject(voteDetails);
				objectOut.flush();
				String message = (String) objectIn.readObject();
				connectionSocket.close();
				this.frame.goToDiplayMessagePanel(message);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			return;
		}

	}
}
